<template>
  <el-container>
    <el-header>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark">
          <h2> {{ title }} </h2>
        </div>
      </el-col>
      <div class="block">
        <span class="demonstration"></span>
        查看记录：
        <el-cascader
          v-model="value"
          :options="options"
          :props="{ expandTrigger: 'hover' }"
          @change="handleChange"></el-cascader>
      </div>
      <!--      <el-row class="caozuo">-->
      <!--        <el-button type="success" round>通过</el-button>-->
      <!--        <el-button type="danger" round>删除</el-button>-->
      <!--      </el-row>-->

    </el-header>
    <el-main>
      <kt-table
        :height="650"
        perms-view="admin"
        :perms-edit="currentUser"
        :perms-delete="currentUser"
        :data="pageResult"
        :columns="tableLabel"
        @findpage="findPage"
        @handleAdd="addelement"
        @handleDelete="deleteElement"
      />
    </el-main>
  </el-container>
</template>

<script>
import KtTable from '@/views/Core/KtTable2'

export default {
  name: 'Record',
  components: {
    KtTable
  },
  data() {
    return {
      tableLabel: [
        {label: '属性', width: '', prop: 'attribute'},
        {label: '单位', width: '', prop: 'util'},
        {label: '值', width: '', prop: 'value'},
        {label: '温度', width: '', prop: 'temperature'},
        {label: '压强', width: '', prop: 'pa'},
        {label: '实测/预测', width: '', prop: 'shiceyuce'},
        {label: '数据来源', width: '', prop: 'source'},
        {label: '备注', width: '', prop: 'remark'}
      ],
      lhsxFlag: true,
      resultFlag: false,
      data: {},
      multipleSelection: [],
      pageRequest: {
        flag: '',

        pageNumber: 1,
        pageSize: 15
      },
      radio: '',
      pageResult: {},
      currentUser: 'guest',
      value: [],
      title: '',
      options: [{
        value: 'addrecord',
        label: '新增记录',
        children: [{
          value: '理化属性表',
          label: '理化属性表'
        }, {
          value: '环境行为表',
          label: '环境行为表'
        }, {
          value: '生态危害表',
          label: '生态危害表'
        }, {
          value: '健康危害表',
          label: '健康危害表'
        }, {
          value: '行业信息表',
          label: '行业信息表'
        }]
      }, {
        value: 'errorecord',
        label: '纠错记录',
        children: [{
          value: '理化属性表',
          label: '理化属性表'
        }, {
          value: '环境行为表',
          label: '环境行为表'
        }, {
          value: '生态危害表',
          label: '生态危害表'
        }, {
          value: '健康危害表',
          label: '健康危害表'
        }, {
          value: '行业信息表',
          label: '行业信息表'
        }]
      }]
    }
  },
  mounted() {
    var user = sessionStorage.getItem('user')
    this.currentUser = user
  },
  methods: {
    findPage: function (data) {
      if (data !== null) {
        this.pageRequest = data.pageRequest
      }
      if (this.value[0] === 'addrecord') {
        this.pageRequest.flag = 0
        if (this.title === '理化属性表') {
          this.$api.addrecord.addRecordAllLhsxb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '环境行为表') {
          this.$api.addrecord.addRecordAllHjxwb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '生态危害表') {
          this.$api.addrecord.addRecordAllStwhb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '健康危害表') {
          this.$api.addrecord.addRecordAllJkwhb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '行业信息表') {
          this.$api.addrecord.addRecordAllHyxxb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        }
      } else {
        this.pageRequest.flag = 2
        if (this.title === '理化属性表') {
          this.$api.addrecord.addRecordAllLhsxb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '环境行为表') {
          this.$api.addrecord.addRecordAllHjxwb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '生态危害表') {
          this.$api.addrecord.addRecordAllStwhb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        } else if (this.title === '健康危害表') {
          this.$api.addrecord.addRecordAllJkwhb(this.pageRequest).then((res) => {
            this.pageResult = res.data
            console.log(res.data)
          }).then(data != null ? data.callback : '')
        } else if (this.title === '行业信息表') {
          this.$api.addrecord.addRecordAllHyxxb(this.pageRequest).then((res) => {
            this.pageResult = res.data
          }).then(data != null ? data.callback : '')
        }
      }
    },
    handleChange(value) {
      this.title = value[1]
      this.radio = value[0]
      if (this.title === '理化属性表') {
        this.tableLabel = [
          {label: '属性', width: '', prop: 'attribute'},
          {label: '单位', width: '', prop: 'util'},
          {label: '值', width: '', prop: 'value'},
          {label: '温度', width: '', prop: 'temperature'},
          {label: '压强', width: '', prop: 'pa'},
          {label: '实测/预测', width: '', prop: 'shiceyuce'},
          {label: '数据来源', width: '', prop: 'source'},
          {label: '备注', width: '', prop: 'remark'}
        ]
      } else if (this.title === '环境行为表') {
        this.tableLabel = [
          {label: '属性', width: '', prop: 'attribute'},
          {label: '介质', width: '', prop: 'medium'},
          {label: '单位', width: '', prop: 'unit'},
          {label: '值', width: '', prop: 'value'},
          {label: '试验时间', width: '', prop: 'time'},
          {label: '试验方法', width: '', prop: 'method'},
          {label: '实测/预测', width: '', prop: 'shiceyuce'},
          {label: '数据来源', width: '', prop: 'source'},
          {label: '备注', width: '', prop: 'remark'}
        ]
      } else if (this.title === '生态危害表') {
        this.tableLabel = [
          {label: '属性', width: '', prop: 'attribute'},
          {label: '实验种属', width: '', prop: 'species'},
          {label: '试验时间', width: '', prop: 'time'},
          {label: '效应类型', width: '', prop: 'type'},
          {label: '效应值单位', width: '', prop: 'unit'},
          {label: '效应值', width: '', prop: 'value'},
          {label: '试验方法', width: '', prop: 'method'},
          {label: '实测/预测', width: '', prop: 'shiceyuce'},
          {label: '数据来源', width: '', prop: 'source'},
          {label: '备注', width: '', prop: 'remark'}
        ]
      } else if (this.title === '健康危害表') {
        this.tableLabel = [
          {label: '属性', width: '', prop: 'attribute'},
          {label: '试验生物种属', width: '', prop: 'species'},
          {label: '试验时间', width: '', prop: 'time'},
          {label: '效应类型', width: '', prop: 'type'},
          {label: '效应值单位', width: '', prop: 'unit'},
          {label: '效应值', width: '', prop: 'value'},
          {label: '致癌性IARC分类', width: '', prop: 'zax'},
          {label: '试验方法', width: '', prop: 'method'},
          {label: '实测/预测', width: '', prop: 'shiceyuce'},
          {label: '数据来源', width: '', prop: 'source'},
          {label: '备注', width: '', prop: 'remark'}
        ]
      } else if (this.title === '行业信息表') {
        this.tableLabel = [
          {label: '参数', width: '', prop: 'attribute'},
          {label: '参数单位', width: '', prop: 'util'},
          {label: '值', width: '', prop: 'value'},
          {label: '时间', width: '', prop: 'time'},
          {label: '数据来源', width: '', prop: 'source'},
          {label: '备注', width: '', prop: 'remark'}
        ]
      }
      this.findPage(null)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteElement: function (params) {
      const id = params.row.id
      this.$confirm('此操作将永久删除该元素, 是否继续?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.title === '理化属性表') {
          this.$api.addrecord.deladdRecordlhsxb(id).then((res) => {
            this.findPage(null)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        } else if (this.title === '环境行为表') {
          this.$api.addrecord.deladdRecordhjxwb(id).then((res) => {
            this.findPage(null)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        } else if (this.title === '生态危害表') {
          this.$api.addrecord.deladdRecordstwhb(id).then((res) => {
            this.findPage(null)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        } else if (this.title === '健康危害表') {
          this.$api.addrecord.deladdRecordjkwhb(id).then((res) => {
            this.findPage(null)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        } else if (this.title === '行业信息表') {
          this.$api.addrecord.deladdRecordhyxxb(id).then((res) => {
            this.findPage(null)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addelement: function (params) {
      params.row.flag = '1'
      const data = params
      console.log("data         `", data)
      this.$confirm('确认入库?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.radio === 'addrecord') {
          console.log("！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！")
          if (this.title === '理化属性表') {
            this.$api.addrecord.addupdatelhsxbFlag(data.row).then((res) => {
              this.findPage(null)
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '环境行为表') {
            this.$api.addrecord.addupdatehjxwbFlag(data.row).then((res) => {
              this.findPage(null)
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '生态危害表') {
            this.$api.addrecord.addupdatestwhbFlag(data.row).then((res) => {
              this.findPage(null)
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '健康危害表') {
            this.$api.addrecord.addupdatejkwhbFlag(data.row).then((res) => {
              this.findPage(null)
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '行业信息表') {
            this.$api.addrecord.addupdatehyxxbFlag(data.row).then((res) => {
              this.findPage(null)
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          }
        } else {
          const lastid = data.row.lastid
          console.log('params.row.lastid', params.row.lastid)
          if (this.title === '理化属性表') {
            this.$api.addrecord.addupdatelhsxbFlag(data.row).then((res) => {
              if (lastid !== '') {
                this.$api.addrecord.deladdRecordlhsxb(lastid).then((res) => {
                  console.log("已经删除!!!!!!", lastid)
                  this.findPage(null)
                })
              } else {
                console.log('zhixnig findpage')
                this.findPage(null)
              }
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '环境行为表') {
            this.$api.addrecord.addupdatehjxwbFlag(data.row).then((res) => {
              if (lastid !== '') {
                this.$api.addrecord.deladdRecordhjxwb(lastid).then((res) => {
                  console.log("已经删除!!!!!!", lastid)
                  this.findPage(null)
                })
              } else {
                console.log('zhixnig findpage')
                this.findPage(null)
              }
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '生态危害表') {
            this.$api.addrecord.addupdatestwhbFlag(data.row).then((res) => {
              if (lastid !== '') {
                this.$api.addrecord.deladdRecordstwhb(lastid).then((res) => {
                  console.log("已经删除!!!!!!", lastid)
                  this.findPage(null)
                })
              } else {
                console.log('zhixnig findpage')
                this.findPage(null)
              }
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '健康危害表') {
            this.$api.addrecord.addupdatejkwhbFlag(data.row).then((res) => {
              if (lastid !== '') {
                this.$api.addrecord.deladdRecordjkwhb(lastid).then((res) => {
                  console.log("已经删除!!!!!!", lastid)
                  this.findPage(null)
                })
              } else {
                console.log('zhixnig findpage')
                this.findPage(null)
              }
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          } else if (this.title === '行业信息表') {
            this.$api.addrecord.addupdatehyxxbFlag(data.row).then((res) => {
              if (lastid !== '') {
                this.$api.addrecord.deladdRecordhyxxb(lastid).then((res) => {
                  console.log("已经删除!!!!!!", lastid)
                  this.findPage(null)
                })
              } else {
                console.log('zhixnig findpage')
                this.findPage(null)
              }
              this.$message({
                type: 'success',
                message: '入库成功!'
              })
            })
          }
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消入库'
        })
      })
    }
  }
}
</script>

<style scoped>
.block {
  position: absolute;

  left: 2px;
}

.caozuo {
  position: absolute;
  right: 2px;
}
</style>
